<?php

namespace Cframe;
use Cframe\Tpllib\Tpl;

class Template extends Tpl{
    protected $assin = array();

    public function assign($name,$value = ''){
        if (is_array($name)){
            foreach ($name as $k=>$v){
                $this->assin[$k] = $v;
            }
        }else{
            $this->assin[$name] = $value;
        }
    }


    public function loadTemplate($name,$paths = ''){
        $filename = $this->convertTemp($name,$paths);
        extract($this->assin);
        include $filename;
    }

    public function display($name = "",array $assign = array(),$paths = ''){
        if (!empty($assign)){
            $this->assign($assign);
        }
        if (is_array($name)){
            if (!is_numeric(array_keys($name)[0])){
                return false;
            }
            $name = arrayToString($name);
        }
        static::loadTemplate($name,$paths);
    }

}

function arrayToString($arr) {
    if (is_object($arr)){
        $arr = (array) $arr;
    }
    if (is_array($arr)){
        return implode('/', array_map([$this,"arrayToString"], $arr));
    }
    return $arr ;
}